/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.cie10.vista;

import modulos.cie10.modelo.Capitulo;
import modulos.cie10.modelo.Categoria;
import modulos.cie10.modelo.Fuente;
import modulos.cie10.modelo.Subcategoria;
import modulos.consulta.vista.pnlDiagnostico;
import vista.frmPrincipal;
import vista.modelo.EnlaceCIE10;
import vista.modelo.OperacionesVarias;

/**
 *
 * @author vic
 */
public class iFrmCie10A extends javax.swing.JInternalFrame {

    int f;
    int c = 0;
    int cat = 0;
    boolean selCat = false;
    boolean selSub = false;
    //para recuperar diagnostico
    private boolean lectura = false;
    private pnlDiagnostico panelDiagnostico;

    /**
     * Creates new form iFrmCie10A
     */
    public iFrmCie10A() {
        initComponents();
        OperacionesVarias.cerrarVentanaEscape(this);
        iniciCombosListas();
        OperacionesVarias.centrar(this);
    }

    public iFrmCie10A(boolean lectura, pnlDiagnostico panel) {
        initComponents();
        iniciCombosListas();
        this.lectura = lectura;
        panelDiagnostico = panel;
        listaSubcategoria.add(menuSubcategoria);
        OperacionesVarias.centrar(this);
    }

    private void iniciCombosListas() {
        EnlaceCIE10.datoCapitulo = true;
        EnlaceCIE10.datoFuente = true;
        EnlaceCIE10.listarTodo();
        if (!EnlaceCIE10.fuentes.isEmpty()) {
            EnlaceCIE10.poneComboFuente(cbFuente, 1);
            fuente = (Fuente) EnlaceCIE10.buscar(cbFuente, 1);
            EnlaceCIE10.poneComboOtros(cbCapitulos, fuente.getIdFuente(), 2);
            listaCategoria.setSelectedIndex(0);
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        menuSubcategoria = new javax.swing.JPopupMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        lblCodigos = new javax.swing.JLabel();
        lblFuente = new javax.swing.JLabel();
        cbFuente = new javax.swing.JComboBox();
        lblCapitulos = new javax.swing.JLabel();
        cbCapitulos = new javax.swing.JComboBox();
        lblCategoria = new javax.swing.JLabel();
        lblSubcategoria = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        txtBuscar = new javax.swing.JTextField();
        btnCerrar = new javax.swing.JButton();
        btnNuevo = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        listaCategoria = new javax.swing.JList();
        jScrollPane4 = new javax.swing.JScrollPane();
        listaSubcategoria = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();

        jMenuItem1.setText("Agregar al diagnostico");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        menuSubcategoria.add(jMenuItem1);

        setTitle("CODIGOS  -DIAGNOSTICO");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/Stethoscope.png"))); // NOI18N

        lblCodigos.setText("Codigos de Clasificacion Diagnostica");

        lblFuente.setText("Fuente:");

        cbFuente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbFuenteActionPerformed(evt);
            }
        });

        lblCapitulos.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        lblCapitulos.setText("Capitulos:");

        cbCapitulos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbCapitulosActionPerformed(evt);
            }
        });

        lblCategoria.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        lblCategoria.setText("Categoria");

        lblSubcategoria.setFont(new java.awt.Font("Ubuntu", 1, 15)); // NOI18N
        lblSubcategoria.setText("Subcategoria");

        txtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtBuscarKeyReleased(evt);
            }
        });

        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        btnCerrar.setText("Cerrar");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });

        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/add.png"))); // NOI18N
        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        listaCategoria.setBackground(new java.awt.Color(209, 230, 239));
        listaCategoria.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listaCategoriaValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(listaCategoria);

        listaSubcategoria.setBackground(new java.awt.Color(209, 230, 239));
        listaSubcategoria.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                listaSubcategoriaMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(listaSubcategoria);

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 14)); // NOI18N
        jLabel1.setText("Buscar:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(btnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(482, 482, 482)
                                .addComponent(btnCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(lblCodigos, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(12, 12, 12)
                                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 649, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(12, 12, 12)
                                    .addComponent(lblCapitulos))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(12, 12, 12)
                                    .addComponent(cbCapitulos, javax.swing.GroupLayout.PREFERRED_SIZE, 649, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(12, 12, 12)
                                    .addComponent(lblCategoria))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(10, 10, 10)
                                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 650, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(12, 12, 12)
                                    .addComponent(lblSubcategoria))
                                .addGroup(layout.createSequentialGroup()
                                    .addGap(10, 10, 10)
                                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 650, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 661, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblFuente, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(4, 4, 4)
                                .addComponent(cbFuente, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addGap(53, 53, 53))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cbFuente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblFuente))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(lblCodigos)))
                .addGap(4, 4, 4)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(lblCapitulos)
                .addGap(6, 6, 6)
                .addComponent(cbCapitulos, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(lblCategoria)
                .addGap(5, 5, 5)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addComponent(lblSubcategoria)
                .addGap(8, 8, 8)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNuevo)
                    .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(btnCerrar))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-692)/2, (screenSize.height-499)/2, 692, 499);
    }// </editor-fold>//GEN-END:initComponents

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        EnlaceCIE10.datoCapitulo = false;
        EnlaceCIE10.datoFuente = false;
        iFrmCie10B fb = new iFrmCie10B();
        frmPrincipal.dskpPrincipal.add(fb);
        fb.setVisible(true);

    }//GEN-LAST:event_btnNuevoActionPerformed

    private void cbCapitulosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbCapitulosActionPerformed
        capitulo = (Capitulo) EnlaceCIE10.buscar(cbCapitulos, 2);
        if (capitulo != null) {
            EnlaceCIE10.poneListaCategoria(listaCategoria, capitulo.getIdCapitulo(), 3);
        } else {
            EnlaceCIE10.dlmCategoria.removeAllElements();
            EnlaceCIE10.dlmSubCategoria.removeAllElements();
        }
    }//GEN-LAST:event_cbCapitulosActionPerformed

    private void cbFuenteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbFuenteActionPerformed
        fuente = (Fuente) EnlaceCIE10.buscar(cbFuente, 1);
        if (fuente != null) {
            EnlaceCIE10.poneComboOtros(cbCapitulos, fuente.getIdFuente(), 2);
            EnlaceCIE10.datoCapitulo = true;
        }

    }//GEN-LAST:event_cbFuenteActionPerformed

    private void listaCategoriaValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listaCategoriaValueChanged
        int index = listaCategoria.getSelectedIndex();
        //System.out.println(index);
        categoria = (Categoria) EnlaceCIE10.buscarLis(index);
        if (categoria != null) {
            EnlaceCIE10.poneListaSubCategoria(listaSubcategoria, categoria.getIdCategoria(), 4);
//            EnlaceCIE10.poneListaSubCategoria(listaSubcategoria, categoria.getCapitulo().getFuente().getIdFuente(), 4);
        } else {
            EnlaceCIE10.dlmSubCategoria.removeAllElements();
        }

    }//GEN-LAST:event_listaCategoriaValueChanged

    private void txtBuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyReleased
        if (categoria != null) {
//            EnlaceCIE10.buscarDatos(listaSubcategoria, categoria.getIdCategoria(), txtBuscar.getText());
            EnlaceCIE10.busquedaPredictiva(listaSubcategoria, categoria.getCapitulo().getFuente().getIdFuente(), txtBuscar.getText());
            
        }

    }//GEN-LAST:event_txtBuscarKeyReleased

    private void listaSubcategoriaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_listaSubcategoriaMouseClicked
        if (lectura) {
            if (evt.getClickCount() == 2) {
                int index = listaSubcategoria.getSelectedIndex();
                if (index != -1) {
                    String str = (String) listaSubcategoria.getSelectedValue();
                    panelDiagnostico.setDiagnostico(str);
                    dispose();
                }
            }
        }
    }//GEN-LAST:event_listaSubcategoriaMouseClicked

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        if (lectura) {
            int index = listaSubcategoria.getSelectedIndex();
            if (index != -1) {
                String str = (String) listaSubcategoria.getSelectedValue();
                panelDiagnostico.setDiagnostico(str);
                dispose();
            }
        }
    }//GEN-LAST:event_jMenuItem1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JComboBox cbCapitulos;
    public static javax.swing.JComboBox cbFuente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel lblCapitulos;
    private javax.swing.JLabel lblCategoria;
    private javax.swing.JLabel lblCodigos;
    private javax.swing.JLabel lblFuente;
    private javax.swing.JLabel lblSubcategoria;
    private javax.swing.JList listaCategoria;
    private javax.swing.JList listaSubcategoria;
    private javax.swing.JPopupMenu menuSubcategoria;
    private javax.swing.JTextField txtBuscar;
    // End of variables declaration//GEN-END:variables
    Fuente fuente;
    Capitulo capitulo;
    Categoria categoria;
    Subcategoria subcategoria;
}
